﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.MyDialogs;
using System.Threading;
using QLNH.QLNH_BUS_WS;

namespace QLNH.MyForms
{
    public partial class AdminCatalogManagement : FormUC
    {
        #region Properties

        private CatalogDLG catalogDLG = null;

        #endregion

        #region Constructor

        public AdminCatalogManagement(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_CATALOG_MANAGEMENT;
        }

        #endregion

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }

        //lay danh sach catalog
        private void GetAllCatalogDetail()
        {
            this.lv_catalog.Items.Clear();

            Thread t = new Thread(new ThreadStart(GetAllCatalogDetailThread));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        List<CatalogDetailDTO> _catalogList = null;
        private void GetAllCatalogDetailThread()
        {
            if (_catalogList == null || _getFromDB == true)
            {
                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                CatalogDetailDTO[] temp = ws.GetAllCatalogDetail();
                _catalogList = temp.ToList<CatalogDetailDTO>();
                _getFromDB = false;
            }
           
            for (int i = 0; i < _catalogList.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.Text = _catalogList[i].ID;
                li.Name = _catalogList[i].ID;
                li.SubItems.Add(_catalogList[i].Name);
                li.SubItems.Add(_catalogList[i].TotalUnit.ToString());

                if (this.lv_catalog.Items.Count % 2 != 0)
                {
                    li.BackColor = Color.LightGray;
                }

                this.lv_catalog.BeginInvoke((MethodInvoker)(() => this.lv_catalog.Items.Add(li)));
                Thread.Sleep(50);
            }
        }

        //them catalog
        private void AddCatalog(CatalogDTO catalog)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.AddCatalog(catalog) == true)
            {
                MessageBox.Show("Thêm catalog thành công");
                _getFromDB = true;
                GetAllCatalogDetail();
            }
            else
            {
                MessageBox.Show("Thêm catalog thất bại");
            }
        }

        //chinh sua catalog
        private bool _getFromDB = false;
        private void EditCatalog(CatalogDetailDTO catalogDetail)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.EditCatalog(catalogDetail) == true)
            {
                MessageBox.Show("Cập nhật catalog thành công");
                _getFromDB = true;
                GetAllCatalogDetail();
            }
            else
            {
                MessageBox.Show("Cập nhật catalog thất bại");
            }
        }

        //xoa catalog
        private void RemoveCatalog(string id)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.RemoveCatalog(id) == true)
            {
                _catalogList.Remove(_catalogList.Find(e => e.ID.Equals(id)));
                MessageBox.Show("Xóa catalog thành công");
                _getFromDB = false;
                GetAllCatalogDetail();
            }
            else
            {
                MessageBox.Show("Xóa catalog thất bại");
            }
        }

        #endregion

        #region Control Events

        private void AdminCatalogManagement_Load(object sender, EventArgs e)
        {
            GetAllCatalogDetail();
            this.tooltip.Show("Nhắp chuột vào đây để sắp xếp lại", this.lv_catalog, new Point(0, 0), 1000);
        }

        private void cmt_action(object sender, EventArgs e)
        {
            int id = Int32.Parse(((ToolStripMenuItem)sender).Tag.ToString());
            switch (id)
            {
                case 0:
                    if (this.catalogDLG == null || this.catalogDLG.IsDisposed)
                        this.catalogDLG = new CatalogDLG();
                    this.catalogDLG.Edit = true;

                    this.catalogDLG.tb_catalogName.Text = this.lv_catalog.SelectedItems[0].SubItems[1].Text;
                    if (this.catalogDLG.ShowDialog() == DialogResult.OK)
                    {
                        string catalogID = this.lv_catalog.SelectedItems[0].Name.ToString();
                        string catalogName = this.catalogDLG.tb_catalogName.Text;
                        CatalogDetailDTO catalog = new CatalogDetailDTO();
                        catalog.ID = catalogID;
                        catalog.Name = catalogName;
                        EditCatalog(catalog);
                    }
                    break;
                case 1:
                    if (MessageBox.Show("Bạn có chắc chắn muốn xóa catalog này và các món thuộc catalog này?", "Nhắc nhở", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        string catalogID = this.lv_catalog.SelectedItems[0].Name.ToString();
                        RemoveCatalog(catalogID);
                    }
                    break;
                default: 
                    break;
            }
        }

        private void lv_catalog_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.lv_catalog.SelectedItems.Count > 0)
            {
                this.cmt_rightClick.Show(this.lv_catalog, e.Location);
            }
        }

        private ListViewItemComparer sorter;
        private bool ascending = true;
        private void lv_catalog_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            sorter = new ListViewItemComparer(e.Column);
            sorter.OrderOfSort = (ascending == true) ? SortOrder.Ascending : SortOrder.Descending;
            if (e.Column == 2)
                sorter.Numeric = true;
            this.lv_catalog.ListViewItemSorter = sorter;
            this.lv_catalog.Sort();
            ascending = !ascending;
        }

        private void bt_back_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_MANAGEMENT);
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            if (this.catalogDLG == null || this.catalogDLG.IsDisposed)
                this.catalogDLG = new CatalogDLG();

            this.catalogDLG.Edit = false;
            if (this.catalogDLG.ShowDialog() == DialogResult.OK)
            {
                string catalogName = this.catalogDLG.tb_catalogName.Text.Trim();
                CatalogDTO catalog = new CatalogDTO();
                catalog.Name = catalogName;
                AddCatalog(catalog);
            }
        }
        #endregion
    }
}
